package com.houspace.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class HttpClientUtils {
    private static final String HTTPS = "https";
    private static HttpClient httpClient;

    public HttpClientUtils() {
    }

    private static SSLSocketFactory getSSLSocketFactory() {
        SSLSocketFactory.getSocketFactory();
        X509TrustManager tm = new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        try {
            SSLContext ctx = SSLContext.getInstance("TLS");
            ctx.init(null, new TrustManager[]{tm},  null);
            SSLSocketFactory socketFactory = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            return socketFactory;
        } catch (Exception var3) {
            throw new IllegalStateException(var3);
        }
    }

    public static <T> T doGet(String url, Class<T> t) throws IOException {
        HttpGet get = new HttpGet(url);

        try {
            HttpResponse response = httpClient.execute(get);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode < 300) {
                HttpEntity entity = response.getEntity();
                T res = JSONUtils.parse(entity.getContent(), t);
                EntityUtils.consume(entity);
                return res;
            } else {
                throw new IOException("http get[" + url + "] failed,statuCode [" + statusCode + "].");
            }
        } catch (Exception var7) {
            if (!get.isAborted()) {
                get.abort();
            }

            throw new IOException(var7);
        }
    }

    public static HttpStreamResponse doGetAsInputStream(String url) throws IOException {
        HttpGet get = new HttpGet(url);

        try {
            HttpResponse response = httpClient.execute(get);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode < 300) {
                HttpEntity entity = response.getEntity();
                return new HttpStreamResponse(entity.getContentType().getValue(), entity.getContentLength(), entity.getContent());
            } else {
                throw new IOException("http get[" + url + "] failed,statuCode [" + statusCode + "].");
            }
        } catch (Exception var6) {
            if (!get.isAborted()) {
                get.abort();
            }

            throw new IOException(var6);
        }
    }

    public static String doGetByEncoding(String url, String encoding) throws IOException {
        HttpGet get = new HttpGet(url);

        try {
            HttpResponse response = httpClient.execute(get);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode < 300) {
                HttpEntity entity = response.getEntity();
                String res = EntityUtils.toString(entity, encoding);
                EntityUtils.consume(entity);
                return res;
            } else {
                throw new IOException("http get[" + url + "] failed,statuCode [" + statusCode + "].");
            }
        } catch (Exception var7) {
            if (!get.isAborted()) {
                get.abort();
            }

            throw new IOException(var7);
        }
    }

    public static String doGet(String url) throws IOException {
        HttpGet get = new HttpGet(url);

        try {
            HttpResponse response = httpClient.execute(get);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode < 300) {
                HttpEntity entity = response.getEntity();
                String res = EntityUtils.toString(entity);
                EntityUtils.consume(entity);
                return res;
            } else {
                throw new IOException("http get[" + url + "] failed,statuCode [" + statusCode + "].");
            }
        } catch (Exception var6) {
            if (!get.isAborted()) {
                get.abort();
            }

            throw new IOException(var6);
        }
    }

    public static String doPostJson(String url, Object body) throws IOException {
        return doPostJson(url, body, (String) null);
    }

    public static String doPostJson(String url, Object body, String encode) throws IOException {
        HttpPost post = new HttpPost(url);

        try {
            if (body != null) {
                post.setEntity(new StringEntity(JSONUtils.toString(body), ContentType.APPLICATION_JSON));
            }

            HttpResponse response = httpClient.execute(post);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode < 300) {
                HttpEntity entity = response.getEntity();
                String res;
                if (encode != null) {
                    res = EntityUtils.toString(entity, encode);
                } else {
                    res = EntityUtils.toString(entity);
                }

                EntityUtils.consume(entity);
                return res;
            } else {
                throw new IOException("http get[" + url + "] failed,statuCode [" + statusCode + "].");
            }
        } catch (Exception var8) {
            if (!post.isAborted()) {
                post.abort();
            }

            throw new IOException(var8);
        }
    }

    static {
        PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
        cm.setMaxTotal(50);
        cm.setDefaultMaxPerRoute(50);
        cm.getSchemeRegistry().register(new Scheme("https", 443, getSSLSocketFactory()));
        DefaultHttpClient defaultHttpClient = new DefaultHttpClient(cm);
        defaultHttpClient.getParams().setIntParameter("http.socket.timeout", 10000);
        httpClient = defaultHttpClient;
    }
}